<!-- subpkg_archive/list/index.vue -->
<script lang="ts" setup>
import { delPatientApi, getPatientListApi } from "@/apis/patient"
import { useConsultStore } from "@/stores/consult"
import type { PatientItem } from "@/types/patient"
import { onPageShow } from "@dcloudio/uni-app"
import { ref } from "vue"

// 列表数据
const patientList = ref<PatientItem[]>([])

// 获取列表数据
const getPatientList = async () => {
  const res = await getPatientListApi()
  patientList.value = res

  // 设置默认患者高亮
  const defaultPatient = patientList.value.find(
    (item) => item.defaultFlag === 1
  )
  if (defaultPatient) {
    consultStore.patientId = defaultPatient.id
  }
}

// 页面显示时刷新数据，因为新增后会返回列表页，所以要刷新数据
onPageShow(() => {
  getPatientList()
})

// 左滑删除配置
const swipeOptions = ref([
  {
    text: "删除",
    style: {
      backgroundColor: "#dd524d",
      color: "",
      fontSize: "",
    },
  },
])

// 侧滑删除操作的id
const activeId = ref("")
// 左滑删除点击事件
const onSwipeActionClick = async (id: string) => {
  // 设置当前操作的id
  activeId.value = id
  // 二次确认删除弹窗
  const res = await uni.showModal({
    title: "是否删除？",
  })
  // 清除 id
  activeId.value = ""
  // 确认删除
  if (res.confirm) {
    // 删除患者
    await delPatientApi(id)
    // 刷新数据 重新请求 -> 本地删除
    // await getPatientList()
    patientList.value = patientList.value.filter((item) => item.id !== id)
    // 轻提示
    uni.showToast({ title: "删除成功", icon: "none" })
    // 删除选中患者时 重置患者id
    if (consultStore.patientId === id) {
      consultStore.patientId = ""
    }
  }
}
// ---------------------  选择患者  ---------------------
// 获取页面参数
const pageParams = defineProps<{ type?: "consult" }>()
// 设置标题
uni.setNavigationBarTitle({
  title: pageParams.type === "consult" ? "选择患者" : "患者列表",
})
// 获取仓库
const consultStore = useConsultStore()
// 设置患者id
const selectPatient = (id: string) => {
  if (pageParams.type === "consult") {
    consultStore.patientId = id
  }
}
// 下一步 跳转 等待付款页面
const nextStep = () => {
  uni.navigateTo({ url: "/subpkg_consult/payment/index" })
}
// ---------------------  选择患者  ---------------------
</script>

<template>
  <scroll-view>
    <view class="archive-page">
      <view v-if="pageParams.type === 'consult'">
        <view>请选择患者信息</view>
        <view class="archive-tips">
          以便医生给出更准确的治疗，信息仅医生可见
        </view>
      </view>
      <view v-else class="archive-tips">最多可添加6人</view>

      <uni-swipe-action>
        <uni-swipe-action-item
          v-for="item in patientList"
          :key="item.id"
          :right-options="swipeOptions"
          @click="onSwipeActionClick(item.id)"
          :show="activeId === item.id ? 'right' : 'none'"
        >
          <view
            @click="selectPatient(item.id)"
            class="archive-card"
            :class="{
              active:
                pageParams.type === 'consult' &&
                consultStore.patientId === item.id,
            }"
          >
            <view class="archive-info">
              <text class="name">{{ item.name }}</text>
              <text class="id-card">
                {{ item.idCard.replace(/^(.{6}).+(.{4})$/, "$1***********$2") }}
              </text>
              <text v-if="item.defaultFlag === 1" class="default">默认</text>
            </view>
            <view class="archive-info">
              <text class="gender">{{ item.gender === 1 ? "男" : "女" }}</text>
              <text class="age">{{ item.age }}岁</text>
            </view>
            <navigator
              hover-class="none"
              class="edit-link"
              :url="`/subpkg_archive/form/index?id=${item.id}`"
            >
              <uni-icons
                type="icon-edit"
                size="20"
                color="#16C2A3"
                custom-prefix="iconfont"
              />
            </navigator>
          </view>
        </uni-swipe-action-item>
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-if="patientList.length < 6" class="archive-card">
        <navigator
          class="add-link"
          hover-class="none"
          url="/subpkg_archive/form/index"
        >
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label">添加患者</text>
        </navigator>
      </view>
      <!-- 下一步操作 -->
      <view v-if="pageParams.type === 'consult'">
        <button
          :disabled="!consultStore.patientId"
          class="uni-button"
          @click="nextStep()"
        >
          下一步
        </button>
      </view>
    </view>
  </scroll-view>
</template>

<style lang="scss">
@import "./index.scss";
</style>
